<?php
namespace App\Http\Controllers;

use Illuminate\Support\Facades\Input;
use App\Services\AppVersionService;

/**
 *
 * 版本控制器
 *
 * @author maqu
 *
 */
class VersionController extends BaseController {

    /**
     *版本更新
     *
     * @return mixed
     */
    public function postUpgrade(){

        //check args
        $platform = Input::get('platform',0);
        $current_ver = Input::get('version_code');

        if(!$current_ver){
            return $this->args_invalid();
        }

        //main
        $service = new AppVersionService();
        $results = $service->checkUpgrade($platform,$current_ver);

        $data = ['upgrade_flg'=>0];

        //edit return values
        if($results!==false){
            $data['upgrade_flg']=1;
            $data = array_merge($data,$results);
        }

        unset($service);
        unset($results);

        return $this->success($data);
    }

    /**
     * 下载最新版本
     *
     * @return mixed
     */
    public function download(){
        Header("Content-type: charset=UTF8");
        $service = new AppVersionService();
        $results = $service->checkUpgrade(0,0);
        if($results){
            $file_name = "sellmeishi.apk";
            $file_dir = $results['download_url'];
            $file = @ fopen($file_dir,"r");

            if (!$file) {
                echo "未找到对应的版本下载";

            } else {

                Header("Content-type: application/octet-stream");

                Header("Content-Disposition: attachment; filename=" . $file_name);

                while (!feof ($file)) {

                    echo fread($file,50000);

                }

                fclose ($file);

            }
        }else{
            echo '未找到对应的版本下载';
        }

    }
}
